package com.he.combinationmodel.composite;

import com.he.combinationmodel.compoent.Compoent;

import java.util.ArrayList;
import java.util.List;

/**
 * 组合模式的树枝节点表示，包含了compoent对象集合的引用，用来增加和删除节点
 */
public class Composite extends Compoent {
    private String name;


    private List<Compoent> compoents = new ArrayList<>();

    public Composite(String name) {
        super(name);
        this.name = name;
    }

    @Override
    public void add(Compoent compoent) {
        compoents.add(compoent);
    }

    @Override
    public void remove(Compoent compoent) {
        compoent.remove(compoent);
    }

    @Override
    public void display(int depth) {
        for (int i = 0; i < depth; i++) {
            System.out.printf("-");
        }
        System.out.printf(name);
        System.out.println();
        for (Compoent compoent : compoents) {
            compoent.display(depth + 1);
        }
    }
}
